package org.credo.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class CallableAndFuture implements Callable<Integer>{

	public static void main(String[] args) {
		CallableAndFuture caf=new CallableAndFuture();
		FutureTask<Integer> future=new FutureTask<Integer>(caf);
		for(int i=0;i<100;i++){
			System.out.println(Thread.currentThread().getName()+"的I的值是:"+i);
			if(i==20){
				new Thread(future, "有返回值的线程").start();
			}
		}
		try {
			System.out.println("子线程的返回值:"+future.get());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public Integer call() throws Exception {
		int i=0;
		for(;i<100;i++){
			System.out.println(Thread.currentThread().getName()+"的I的值为:"+i);
		}
		return i;
	}

}
